package br.uniriotec.pssgbd.unterhaltung.model.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import br.uniriotec.pssgbd.unterhaltung.model.entidade.Temperatura;

public class TemperaturaDAO extends GenericDAO{
	private static String sqlSelect = "SELECT cod_temp, maxima, minima FROM temperatura ";
	
	public Temperatura obterTemperatura(String sqlWhere, int codTemp) throws SQLException, ClassNotFoundException{
		this.criaConexao(false);
		
		String sqlCompleta = sqlSelect + sqlWhere;
		
		PreparedStatement stmt = null;
		ResultSet rs = null;
		Temperatura temp = null;
		
		try{
			stmt = getConnection().prepareStatement(sqlCompleta);
			stmt.setInt(1, codTemp);
			rs = stmt.executeQuery();
			if (rs.next()){
				temp = new Temperatura();
				temp.setCodTemp(rs.getInt("cod_temp"));
				temp.setTempMax(rs.getDouble("maxima"));
				temp.setTempMin(rs.getDouble("minima"));
			}
			
		}
		
		catch(SQLException e){
			throw e;
		}
		
		finally{
			try{
				stmt.close();
				rs.close();
				this.fechaConexao();
			} 
			catch (SQLException e) {
				throw e;
			}
		}
		
		return temp;
	}

	public List<Temperatura> obterTemperaturas() throws SQLException, ClassNotFoundException{
		this.criaConexao(false);
		
		PreparedStatement stmt = null;
		ResultSet rs = null;
		Temperatura temp = null;
		List<Temperatura> listaTemps = new ArrayList<Temperatura>();
		
		try{
			stmt = getConnection().prepareStatement(sqlSelect);
			rs = stmt.executeQuery();
			while (rs.next()){
				temp = new Temperatura();
				temp.setCodTemp(rs.getInt("cod_temp"));
				temp.setTempMax(rs.getDouble("maxima"));
				temp.setTempMin(rs.getDouble("minima"));
				listaTemps.add(temp);
			}
			
		}
		
		catch(SQLException e){
			throw e;
		}
		
		finally{
			try{
				stmt.close();
				rs.close();
				this.fechaConexao();
			} 
			catch (SQLException e) {
				throw e;
			}
		}
		
		return listaTemps;
	}
	
	/**
	 * @return the sqlSelect
	 */
 	public String getSqlSelect() {
		return sqlSelect;
	}
}
